package com.bysj.sausys.controller.backend;

import com.bysj.sausys.common.JsonData;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.model.SysDicParams;
import com.bysj.sausys.service.ISysDicParamsService;
import com.bysj.sausys.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户controller
 *
 * @author WangZK
 */
@Api(description = "用户接口")
@Controller
public class UserController {

    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISysDicParamsService sysDicParamsService;

    private final String pwdStr = "password";
    private final String statusStr = "status";

    @ApiOperation(value = "用户注销接口")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public JsonData logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return JsonData.success();
    }

    /**
     * 登录操作
     *
     * @param username
     * @param password
     * @param request
     * @return
     */
    @ApiOperation(value = "用户登录接口")
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public JsonData login(@ApiParam(value = "用户名", required = true) @RequestParam String username,
                          @ApiParam(value = "密码", required = true) @RequestParam String password,
                          HttpServletRequest request) {
        Map map = sysUserService.findByUsername(username);

        if (StringUtils.isBlank(username)) {
            return JsonData.fail("用户名不能为空");
        } else if (StringUtils.isBlank(password)) {
            return JsonData.fail("密码不能为空");
        } else if (MapUtils.isEmpty(map)) {
            return JsonData.fail("查询不到指定的用户");
        } else if (!password.equals(map.get(this.pwdStr))) {
            return JsonData.fail("用户名或密码错误");
        } else if (((int) map.get(this.statusStr)) != 1) {
            return JsonData.fail("用户已被禁用，请联系管理员");
        } else {
            // 通过校验
            map.remove("password");
            SysDicParams dicParams = sysDicParamsService.findByPmname((String) map.get("categoryId"));
            if (dicParams == null) {
                return JsonData.fail("登录失败，用户异常");
            } else {
                map.put("categoryValue", dicParams.getPmvalue());
            }
            request.getSession().setAttribute(UserConst.CURRENT_USER, map);
            return JsonData.success(map, "登录成功");
        }
    }

    /**
     * 判断用户是否已经登录
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "判断用户是否已经登录接口")
    @RequestMapping(value = "/checkLogin", method = RequestMethod.GET)
    @ResponseBody
    public JsonData checkLogin(HttpServletRequest request) {
        Map map = UserConst.getCurrentUser(request);
        if (map == null) {
            return JsonData.fail("用户没有登录");
        }
        return JsonData.success(map);
    }
}
